#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlResult>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle("kehu");

    m_from = new Form;
    ui->le_2->setEchoMode(QLineEdit::Password);

    //设置驱动
    m_db = QSqlDatabase::addDatabase("QODBC");
    QString dsn = "Driver={MySQL ODBC 8.0 ANSI Driver}";
    m_db.setDatabaseName(dsn);

    //连接数据库
    m_db.setHostName("localhost");
    m_db.setPort(3306);
    m_db.setUserName("root");
    m_db.setPassword("root");

    bool ret = m_db.open();
    if(ret == false)
    {
        qDebug() << "connect err:"<< m_db.lastError().text();
        return ;
    }

    qDebug()<<"connect ok";

    QSqlQuery query(m_db);

}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_bt_1_clicked()
{
    QString godId = ui->le_1->text();
    QString password = ui->le_2->text();

    //从数据库中读取数据
    QSqlQuery query(m_db);

    QString qlk = QString("use testDB");
    int ret1 = query.exec(qlk);
    if(ret1 == false)
    {
        qDebug()<<"use err";
    }


    // 检查输入是否有效（如非空、数字等）
    if (godId.isEmpty()  || password.isEmpty()) {
        QMessageBox::warning(this, "警告", "输入无效！");
        return;
    }

    int userId = godId.toInt();

    // 使用预处理语句防止SQL注入
    query.prepare("SELECT EXISTS (SELECT 1 FROM kehu WHERE god_id = :godId) AS result");
    query.bindValue(":godId", userId);
    if (!query.exec()) {
        qDebug() << "查询执行失败";
        QMessageBox::warning(this, "警告", "数据库查询失败！");
        return;
    }

    if (!query.next() || !query.value("result").toBool()) {
        qDebug() << "用户不存在";
        QMessageBox::warning(this, "警告", "账号不存在，请注册！");
        return;
    }

    query.prepare("SELECT EXISTS (SELECT 1 FROM kehu WHERE name = :password) AS result");
    query.bindValue(":password", password);
    if (!query.exec()) {
        qDebug() << "查询执行失败";
        QMessageBox::warning(this, "警告", "数据库查询失败！");
        return;
    }

    if (!query.next() || !query.value("result").toBool()) {
        qDebug() << "密码错误";
        QMessageBox::warning(this, "警告", "密码错误！");
        return;
    }

    qDebug() << "登录成功";
    QMessageBox::information(this, "通知", "登录成功");
    m_from->show();
    this->hide();
    m_db.close();  //清理工作


}

void MainWindow::on_bt_2_clicked()
{
    QString str1 = ui->le_1->text();
    int i = str1.toInt();
    QString str2 = ui->le_2->text();
    QSqlQuery query(m_db);
    QString qlk = QString("use testDB");
    int ret1 = query.exec(qlk);
    if(ret1 == false)
    {
        qDebug()<<"use err";
    }
    QString qtn("kehu");
    //表中插入数据
    QString sql = QString("insert into %1 values (%2,'%3')").arg(qtn).arg(i).arg(str2);
    bool ret = query.exec(sql);
    if(ret == false)
    {
        int err = query.lastError().nativeErrorCode().toInt();
        if(err != 1062) //主键重复
            qDebug()<<"insert table err"<<query.lastError().text();
        QMessageBox::warning(this,"警告","账户已存在!");
    }
    if(ret == true)
    {
        QMessageBox::information(this,"通知","注册成功");
    }
    m_db.close();  //清理工作
}
